//
//  TRNewGoodsViewController.m
//  WeiMiLan
//
//  Created by Mac on 14-7-19.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRNewGoodsViewController.h"
#import "TRNewsCell.h"
#import "RankListCell.h"
#import "TRSendWebViewController.h"
@interface TRNewGoodsViewController ()<PullTableViewDelegate>

@property(nonatomic,strong)NSMutableDictionary * pushDict;
@property(nonatomic,strong)NSArray * array3005;
@property(nonatomic,strong)NSArray * array3006;
@property(nonatomic,strong)NSMutableArray * currentArray;
@property(nonatomic,strong)NSDictionary * pageInfo;


@end

@implementation TRNewGoodsViewController
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView{
    
    [self performSelector:@selector(reflushNow) withObject:nil afterDelay:1.];
}
-(void)reflushNow{
    self.tableView.pullTableIsRefreshing=NO;
    self.tableView.pullTableIsLoadingMore=NO;
}
- (void)pullTableViewDidTriggerLoadMore:(PullTableView*)pullTableView{
    
     [self performSelector:@selector(reflushNow) withObject:nil afterDelay:2.];
    
    if (self.pageInfo) {
        if ([self.pageInfo[@"NUMBER"] intValue]==[self.pageInfo[@"PAGES"] intValue]) {
            [SVProgressHUD showImage:nil status:@"已无更多"];
        }else{
        
//        [self pushApi:[self.pageInfo[@"NEXT"] intValue]];
        }
    }
//    [self pushApi:];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tableView.pullTableIsRefreshing= YES;
    [self performSelector:@selector(reflushNow) withObject:nil afterDelay:1.];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    

   
//    self.tableView.rowHeight = 243.f;
    
    self.tableView.pullBackgroundColor = [UIColor whiteColor];
    self.tableView.pullTextColor = [UIColor blackColor];
    [self initNavigation];
    self.currentArray = [@[]mutableCopy];
//    self.pushDict = [NSStandardUserDefaults objectForKey:@"pushFromServer"];
//   self.array3005 = self.pushDict[@"3005"];
//    self.array3006 = self.pushDict[@"3006"];
//    [self.currentArray addObjectsFromArray:self.array3005];
//     [self.currentArray addObjectsFromArray:self.array3006];
self.currentArray = [NSStandardUserDefaults objectForKey:@"最新动态字典"];
    
   
//    [self pushApi:1];
    
}



-(void)dealloc{
    [self.tableView setDelegate:nil];
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:SERVICE_NAME_RECOMMEND object:nil];
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    self.weChatNavigationBar.titleLabel.text=@"最新动态";
    [self.weChatNavigationBar.rightButton setImage:nil forState:0];
    [self.weChatNavigationBar .leftButton addTarget:self action:@selector(exit)  forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)exit
{
    
    
   [self.navigationController popViewControllerAnimated:YES]; 
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 243.f;
     NSDictionary * pushContent = self.currentArray[indexPath.row];
    if (pushContent[@"ID"]) {
        height = 95.f;
    }else{
        height = 243.f;
    }
    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return self.currentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell;
    NSDictionary * pushContent = self.currentArray[indexPath.row];
    if (pushContent[@"ID"]) {
        [tableView registerNib:[UINib nibWithNibName:@"RankListCell" bundle:nil] forCellReuseIdentifier:@"RankListCell"];
       
        RankListCell *rankListCell = [tableView dequeueReusableCellWithIdentifier:@"RankListCell" forIndexPath:indexPath];
        
        if (rankListCell == nil) {
            NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"RankListCell" owner:self options:nil] ;
            rankListCell = [nib objectAtIndex:0];
        }
        rankListCell.kindLabel.text =pushContent[@"BRAND_NAME"];
        rankListCell.nameLabel.text =pushContent[@"NAME"];
        
        rankListCell.timesLabel.text = @"";
        
        [rankListCell.HeadImageV setImageWithURL:[NSURL URLWithString:pushContent[@"THUMBNAIL_URL"]] placeholderImage:[UIImage imageNamed:@"timeline_image_loading"]];
        
        rankListCell.smallIv.image = [UIImage imageNamed:@"coll收藏按钮"];
        
        cell = rankListCell;
        
    }else if (pushContent[@"html"]) {
         [tableView registerNib:[UINib nibWithNibName:@"TRNewsCell" bundle:nil] forCellReuseIdentifier:@"TRNewsCell"];
        NSString * title = pushContent[@"title"];
        NSString * create_date =  pushContent[@"CREATE_DATE"];
        
        cell =(TRNewsCell * )[tableView dequeueReusableCellWithIdentifier:@"TRNewsCell" forIndexPath:indexPath];
        
        ((TRNewsCell * )cell).timeLabel.text = [self getTime:create_date];
        ((TRNewsCell * )cell).titleLabel.text = title;
        
        [((TRNewsCell * )cell).headImage setImageWithURL:[NSURL URLWithString:pushContent[@"pic"]]];
    }
   
    UIView *v=[[UIView alloc] init];
    v.backgroundColor=[UIColor clearColor];
    
    cell.selectedBackgroundView=v;
    
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    SERVICE_CODE：3001：接收确认 3002：同步 3003：阅读 3004:消息详情
    NSDictionary * pushContent = self.currentArray[indexPath.row];
    if (pushContent[@"ID"]) {
        TRProductDetailViewController *vc = [[TRProductDetailViewController alloc] init];
        vc.productTypeId = pushContent[@"ID"];;
        [self presentViewController:vc animated:YES completion:nil];
        
    }else if (pushContent[@"html"]) {
        TRSendWebViewController * webVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TRSendWebViewController"];
        webVC.pushUrl = [NSURL URLWithString:pushContent[@"html"]];
        webVC.title= @"最新动态";
        [self.navigationController pushViewController:webVC animated:YES];
    }
    

}

-(NSString *)getTime:(NSString *)time{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    
//    [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    
    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
    
    NSDate* inputDate = [inputFormatter dateFromString:time];
    
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    
    [outputFormatter setLocale:[NSLocale currentLocale]];
    
    [outputFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString *str = [outputFormatter stringFromDate:inputDate];
    
    return str;
}
@end
